package com.lu.widgets.component.animation;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

/**
 * @author lrc
 * @Modifier lucifer
 */
public class iTranslateAnimation {
	private static int activityHeight = 0;
	private static int activityWidth = 0;

	private Context context;
	private RelativeLayout layout;
	private RelativeLayout parentLayout;
	private TranslateAnimation flyOutAction;
	private TranslateAnimation flyInAction;
	private float position;
	private int duration = 800;
	private boolean enableBounds = false;
	private boolean enableParent = false;

	private boolean flyIn = false;

	/**
	 * @param context
	 *            Activity
	 * @param layout
	 *            Layout
	 * @param moveDirection
	 * <br>
	 *            case 0: 底部向上飞入 <br>
	 *            case 1: 上部向下飞入 <br>
	 *            case 2: 右部向左飞入 <br>
	 *            case 3: 左部向右飞入
	 * @param duration
	 *            动画时间
	 */
	public iTranslateAnimation(Context context, RelativeLayout layout,
			int moveDirection, int duration) {
		this.context = context;
		this.layout = layout;
		this.duration = duration;
		if (activityHeight == 0 && activityWidth == 0
				&& context instanceof Activity) {
			Display display = ((Activity) context).getWindowManager()
					.getDefaultDisplay();
			activityHeight = display.getHeight();
			activityWidth = display.getWidth();
		}
		setMoveDirection(moveDirection);
	}

	/**
	 * @param context
	 *            Activity
	 * @param layout
	 *            Layout
	 * @param moveDirection
	 * <br>
	 *            case 0: 底部向上飞入 <br>
	 *            case 1: 上部向下飞入 <br>
	 *            case 2: 右部向左飞入 <br>
	 *            case 3: 左部向右飞入
	 * @param duration
	 *            动画时间
	 * @param parentLayout
	 *            父布局
	 * @param enableBounds
	 */
	public iTranslateAnimation(Context context, RelativeLayout layout,
			int moveDirection, int duration, boolean enableBounds) {
		this(context, layout, moveDirection, duration);
		this.enableBounds = enableBounds;
	}

	/**
	 * @param context
	 *            Activity
	 * @param layout
	 *            Layout
	 * @param moveDirection
	 * <br>
	 *            case 0: 底部向上飞入 <br>
	 *            case 1: 上部向下飞入 <br>
	 *            case 2: 右部向左飞入 <br>
	 *            case 3: 左部向右飞入
	 * @param duration
	 *            动画时间
	 * @param parentLayout
	 *            父布局
	 * @param enableBounds
	 */
	public iTranslateAnimation(Context context, RelativeLayout layout,
			int moveDirection, int duration, RelativeLayout parentLayout,
			boolean enableBounds) {
		this(context, layout, moveDirection, duration);
		this.parentLayout = parentLayout;
		this.enableBounds = enableBounds;
		this.enableParent = parentLayout != null;
	}

	private void setMoveDirection(int moveDirection) {
		if (parentLayout != null)
			parentLayout.measure(MeasureSpec.makeMeasureSpec(activityWidth,
					MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(
					activityHeight, MeasureSpec.AT_MOST));
		else
			layout.measure(MeasureSpec.makeMeasureSpec(activityWidth,
					MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(
					activityHeight, MeasureSpec.AT_MOST));
		switch (moveDirection) {
		case 0:
			// 底部向上飞入
			position = (parentLayout == null ? activityHeight : parentLayout
					.getMeasuredHeight())
					/ (float) (layout.getMeasuredHeight());
			setFlyInAnimation(0.0f, 0.0f, position, 0.0f);
			setFlyOutAnimation(0.0f, 0.0f, 0.0f, position);
			break;
		default:
		case 1:
			// 上部向下飞入
			position = 1
					- ((parentLayout == null ? activityHeight : parentLayout
							.getMeasuredHeight()) + layout.getMeasuredHeight())
					/ (float) (layout.getMeasuredHeight());
			setFlyInAnimation(0.0f, 0.0f, position, 0.0f);
			setFlyOutAnimation(0.0f, 0.0f, 0.0f, position);
			break;
		case 2:
			// 右部向左飞入
			position = (parentLayout == null ? activityWidth : parentLayout
					.getMeasuredWidth()) / (float) (layout.getMeasuredWidth());
			setFlyInAnimation(position, 0.0f, 0.0f, 0.0f);
			setFlyOutAnimation(0.0f, position, 0.0f, 0.0f);
			break;
		case 3:
			// 左部向右飞入
			position = 1
					- ((parentLayout == null ? activityWidth : parentLayout
							.getMeasuredWidth()) + layout.getMeasuredWidth())
					/ (float) (layout.getMeasuredWidth());
			setFlyInAnimation(position, 0.0f, 0.0f, 0.0f);
			setFlyOutAnimation(0.0f, position, 0.0f, 0.0f);
			break;
		}
	}

	private void setFlyInAnimation(float fx, float tx, float fy, float ty) {
		flyInAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fx,
				Animation.RELATIVE_TO_SELF, tx, Animation.RELATIVE_TO_SELF, fy,
				Animation.RELATIVE_TO_SELF, ty);
		flyInAction.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation animation) {
				layout.setVisibility(View.VISIBLE);
				if (enableParent) {
					parentLayout.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
				if (enableParent) {
					parentLayout.setVisibility(View.VISIBLE);
				}
			}

		});
		if (enableBounds)
			flyInAction.setInterpolator(AnimationUtils.loadInterpolator(
					context, android.R.anim.overshoot_interpolator));
		flyInAction.setDuration(duration);
	}

	private void setFlyOutAnimation(float fx, float tx, float fy, float ty) {
		flyOutAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fx,
				Animation.RELATIVE_TO_SELF, tx, Animation.RELATIVE_TO_SELF, fy,
				Animation.RELATIVE_TO_SELF, ty);
		flyOutAction.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				layout.setVisibility(View.GONE);
				if (enableParent) {
					parentLayout.setVisibility(View.GONE);
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}

		});
		if (enableBounds)
			flyOutAction.setInterpolator(AnimationUtils.loadInterpolator(
					context, android.R.anim.anticipate_overshoot_interpolator));
		flyOutAction.setDuration(duration);
	}

	public void run() {
		if (flyIn) {
			flyIn = false;
			flyOut();
		} else {
			flyIn = true;
			flyIn();
		}
	}

	public void flyIn() {
		layout.startAnimation(flyInAction);
	}

	public void flyOut() {
		layout.startAnimation(flyOutAction);
	}

	/**
	 * @return the enableParent
	 */
	public boolean isEnableParent() {
		return enableParent;
	}

	/**
	 * @param enableParent
	 *            the enableParent to set
	 */
	public void setEnableParent(boolean enableParent) {
		this.enableParent = (enableParent && parentLayout != null);
	}
}
